
package com.example.quicklaunch;

import android.content.Context;
import android.content.pm.ResolveInfo;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import opensource.jpinyin.PinyinFormat;
import opensource.jpinyin.PinyinHelper;

import java.util.ArrayList;
import java.util.List;

public class AppItem implements Parcelable{
    private String appLable;
    private String lablePinyin;
    private int appDrawable;
    private String appPackage;
    private String appClassName;
    private ResolveInfo resolveInfo;
    private List<Integer> matchedIndex;
    private List<Integer> bitMatchIndex;

    // Letter 2 digit tools. From 9-key keyboard.
    private static final int[] LETTER2DIGIT = {
            2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
            5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9
    };

    public AppItem() {
        clearMatchedIndex();
    }

    public List<Integer> getBitMatchIndex() {
        return bitMatchIndex;
    }

    public void setBitMatchIndex(List<Integer> bitMatchIndex) {
        this.bitMatchIndex = bitMatchIndex;
    }

    public List<Integer> getMatchedIndex() {
        return matchedIndex;
    }

    public void clearMatchedIndex() {
        matchedIndex = null;
        bitMatchIndex = null;
    }

    public void setMatchedIndex(List<Integer> matchedIndex) {
        this.matchedIndex = matchedIndex;
    }

    public String getLablePinyin() {
        return lablePinyin;
    }

    public String getAppLable() {
        return appLable;
    }

    public void setAppLable(String appLable, String labelPinyin) {
        this.appLable = appLable;
        if (TextUtils.isEmpty(labelPinyin)) {
            this.lablePinyin = pinyin(appLable).toUpperCase();
        } else {
            this.lablePinyin = labelPinyin;
        }
    }

    public int getAppDrawable() {
        return appDrawable;
    }

    public void setAppDrawable(int appDrawable) {
        this.appDrawable = appDrawable;
    }

    public String getAppPackage() {
        return appPackage;
    }

    public void setAppPackage(String appPackage) {
        this.appPackage = appPackage;
    }

    public String getAppClassName() {
        return appClassName;
    }

    public void setAppClassName(String appClassName) {
        this.appClassName = appClassName;
    }

    public ResolveInfo getResolveInfo() {
        return resolveInfo;
    }

    public void setResolveInfo(ResolveInfo resolveInfo) {
        this.resolveInfo = resolveInfo;
    }

    @Override
    public String toString() {
        return "AppItem [appLable=" + appLable + ", appDrawable=" + appDrawable
                + ", appPackage=" + appPackage + ", appClassName="
                + appClassName + "]";
    }

    private static String leter2digit(String source) {
        StringBuilder sb = new StringBuilder();
        for (char c : source.toCharArray()) {
            if (c >= 'A' && c <= 'Z') {
                sb.append(LETTER2DIGIT[c - 'A']);
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static final AppItem parse(Context context, ResolveInfo info) {
        AppItem appItem = new AppItem();
        String lable = info.loadLabel(context.getPackageManager()).toString();
        appItem.setAppLable(lable, null);
        appItem.setAppClassName(info.activityInfo.name);
        appItem.setAppPackage(info.activityInfo.packageName);
        appItem.setResolveInfo(info);
        return appItem;
    }

    public static String pinyin(String input) {
        return leter2digit(PinyinHelper.convertToPinyinString(
                input, " ", PinyinFormat.WITHOUT_TONE).toUpperCase());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @SuppressWarnings("unchecked")
    public AppItem(Parcel parcel) {
        appLable = parcel.readString();
        lablePinyin = parcel.readString();
        appDrawable = parcel.readInt();
        appPackage = parcel.readString();
        appClassName = parcel.readString();
        resolveInfo = parcel.readParcelable(ResolveInfo.class.getClassLoader());
        matchedIndex = parcel.readArrayList(ArrayList.class.getClassLoader());
        bitMatchIndex = parcel.readArrayList(ArrayList.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(appLable);
        dest.writeString(lablePinyin);
        dest.writeInt(appDrawable);
        dest.writeString(appPackage);
        dest.writeString(appClassName);
        dest.writeParcelable(resolveInfo, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
        dest.writeList(matchedIndex);
        dest.writeList(bitMatchIndex);
    }
    
    public static final Parcelable.Creator<AppItem> CREATOR = new Creator<AppItem>() {
        @Override
        public AppItem[] newArray(int arg0) {
            return new AppItem[arg0];
        }
        
        @Override
        public AppItem createFromParcel(Parcel arg0) {
            return new AppItem(arg0);
        }
    };
}
